package com.huawei.eidc.message;

import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.print.Doc;

import org.dom4j.Attribute;
import org.dom4j.Branch;
import org.dom4j.CDATA;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.InvalidXPathException;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.QName;
import org.dom4j.Text;
import org.dom4j.Visitor;
import org.dom4j.XPath;
import com.huawei.eidc.message.service.WSDService;
import com.huawei.eidc.message.service.impl.WSDServiceImpl;

public class Soap {
	
	private static WSDService wsd = new WSDServiceImpl();
	private static Soap soap = new Soap();
	private static List<String> parentID = new ArrayList<String>();
	/**
	 * @param args
	 */
	public static void main(String[] args)throws Exception {
		//企业信息查询接口
//		soap.ecInfoQuery("ecInfoQuery");
		
		//企业同步接口
//		Document doc = DocumentHelper.createDocument();
//		Element ec = doc.addElement("BODY").addElement("EC");
//		ec.addElement("ECID").setText("1000");
//		ec.addElement("ECNAME").setText("华为A");
//		ec.addElement("ADDR").setText("光谷软件园");
//		ec.addElement("ECMANNAME").setText("陈先生");
//		ec.addElement("ECTEL").setText("13800138000");
//		ecInfoSync("ecInfoSync", doc);
		
		
		//企业部门查询接口
//		Document doc = DocumentHelper.createDocument();
//		Element body = doc.addElement("BODY");
//		Element page = body.addElement("PAGEINFO");
//		page.addElement("CURRENTPAGE").setText("1");
//		page.addElement("PAGESIZE").setText("10");
//		body.addElement("ECID").setText("1000");
////		body.addElement("DEPTNAME").setText("");	//可选
//		String result = deptQuery("deptQuery",doc);
//		Document document = DocumentHelper.parseText(result);
//		Element deptList = document.getRootElement().element("DEPTLIST");
//		
//		Iterator ite = deptList.elementIterator();
//		List<Element> father = new ArrayList<Element>();
//		while(ite.hasNext()) {
//			Element deptInfo = (Element)ite.next();
//			String pid = deptInfo.elementText("DEPTPARENT");
//			String  did = deptInfo.elementText("DEPTID");
//			if("0".equals(pid)){
//				father.add(deptInfo);
//			}
//			parentID.add(pid);
//		}
//		
//		for (int i = 0; i < father.size(); i++) {
//			String did = father.get(i).elementText("DEPTID");
//			findChild(deptList, "1000");
//		}
//		
		
		//企业部门详情查询接口
//		Map<String, String> map = new TreeMap<String, String>();
//		map.put("DEPTID ", "1332");
//		addressDetail("deptDetail", map);
		
		//企业部门同步
//		Map<String, String> map = new TreeMap<String, String>();
////		map.put("DEPTID ", "1025");
//		map.put("ECID", "1000");	//当操作码为02、03时必填
//		map.put("DEPTNAME", "核心研发");
//		map.put("OPRTYPE", "01");	//操作代码01：新增；02：修改；03：删除
//		map.put("DEPTPARENT", "1025");	//
//		map.put("DEPTLEVEL", "2");	//部门级别
//		map.put("DISPLAYORDER", "");	//界面上显示顺序
//		map.put("REMARK", "负责公司核心模块研发");	//部门描述
//		map.put("DEPTMANAGERID", "1001");//用户ID
//		deptSync("deptSync", map);
		
		//个人信息查询接口
//		Map<String, String> map = new TreeMap<String, String>();
//		map.put("USERID ", "1002");	
//		addressDetail("userInfoQuery", map);
		
		//企业通讯录查询接口(userid必填)文档指定可选
		Document doc = DocumentHelper.createDocument();
		Element body = doc.addElement("BODY");
		Element page = body.addElement("PAGEINFO");
		page.addElement("CURRENTPAGE").setText("1");
		page.addElement("PAGESIZE").setText("10");
		body.addElement("ECID").setText("1000");
		body.addElement("USERID").setText("10043443");	//可选
		body.addElement("DEPTID").setText("1025");	//可选
		addressQuery("addressQuery",doc);
		
		//企业通讯录信息详情查询接口
//		Map<String, String> map = new TreeMap<String, String>();
//		map.put("MEMBERID ", "1067");
//		addressDetail("addressDetail", map);
		
		//3.1.33	企业通讯录同步接口
//		Map<String, String> userMap = new TreeMap<String, String>();
//		Map<String, String> memberMap = new TreeMap<String, String>();
//		userMap.put("USERACCOUNT","lisi");		//用户账户	必填
////		userMap.put("PASSWORD","677898");			//明文密码（选填，不填默认为888888）
//		userMap.put("MSGRECEIVEFLAG","1");		//接受消息标志
//		userMap.put("DUTY","4002");					//岗位（4001：为管理员；4002:普通成员）(必填)
//		
//		memberMap.put("MEMBERID","");			//成员id
//		memberMap.put("USERNAME","李四");			//成员姓名	必填
//		memberMap.put("MEMBERNO","13800138004");			//工号		必填
//		memberMap.put("MOBILE","13800138004");				//手机号		必填
//		memberMap.put("DEPTID","1331");				//部门ID		必填
//		memberMap.put("WORKPHONE","");			//公司座机	
//		memberMap.put("OTHERMOBILE","");		//其它手机
//		memberMap.put("FAX","");				//公司传真
//		memberMap.put("EMALL","");				//企业邮箱
//		memberMap.put("ZIPCODE","");		
//		memberMap.put("QQ","");
//		memberMap.put("VPMNNO","");
//		memberMap.put("LINKNAME","");
//		memberMap.put("TITLE","");
//		memberMap.put("SCHOOL","");
//		memberMap.put("MAJOR","");
//		memberMap.put("CERTNUMBER","");
//		memberMap.put("CERTTYPE","");
//		memberMap.put("PERSONALWEB","");
//		memberMap.put("MARRIAGE","0");	//婚姻状态 2：保密；1:已婚；0：未婚
//		memberMap.put("BIRTHDAY","");
//		memberMap.put("ONDUTYTIME","");
		
//		syncMember("syncMember", userMap, memberMap, "1000", "1001", "0");
		
		
		//个人信息同步
//		Map<String, String> userMap = new TreeMap<String, String>();
//		userMap.put("USERID", "1001");
//		userMap.put("USERACCOUNT", "1267349000");
//		userMap.put("PSWSECONDCONFIRM", "0");
//		userMap.put("SMSGFLAG", "0");
//		userMap.put("EMAILFLAG", "0");
//		userMap.put("DESKMSGFLAG", "0");
//		Map<String, String> memberMap = new TreeMap<String, String>();
//		memberMap.put("MEMBERNAME", "丽丽");
//		memberMap.put("SEX","1");
//		memberMap.put("WORKPHONE", "123");
//		memberMap.put("QQ", "2345543");
//		memberMap.put("MSN", "111msn@163.com");
//		memberMap.put("EMAIL", "xioax@sina.com");
//		memberMap.put("BIRTHDAY","19910101");
//		userInfoSync("userInfoSync", userMap, memberMap);
		
		//个人通讯录查询
//		Document doc = DocumentHelper.createDocument();
//		Element body = doc.addElement("BODY");
//		Element page = body.addElement("PAGEINFO");
//		page.addElement("CURRENTPAGE").setText("1");
//		page.addElement("PAGESIZE").setText("10");
//		body.addElement("ECID").setText("1000");
//		body.addElement("USERID").setText("10");	
////		body.addElement("EGROUPID").setText("1025");	//可选
//		personalAddressQuery("personalAddressQuery",doc); 
		
		//3.1.35个人通讯录同步接口
//		Map<String,String> map = new TreeMap<String, String>();
//		map.put("ECID", "1000");				//企业编号
//		map.put("INDIVIDUALCODE", "11111");		//人员编号
//		map.put("USERID", "1001");				//操作人ID
//		map.put("OPRTYPE", "0");				//操作类型（0 增加 1修改 2删除）
////		map.put("ADDRESSID", "");			//通讯录成员ID（修改、删除时需要）
//		map.put("CUSTOMERNAME", "aaa");		//成员姓名（必填）
//		map.put("MOBILE", "13800138001");				//手机号码（必填）
////		map.put("COMPANYNAME", "");			//公司名
//		map.put("EGROUP", "105");				//群组
////		map.put("SEX", "");					//性别（默认保密）：0 男 1 女 2保密
//		map.put("WORKPHONE", "027-12345678");
//		map.put("HOMEPHONE", "027-12345678");
//		map.put("OTHERMOBILE", "13800138002");
//		map.put("FAX", "");					//公司传真
//		map.put("EMAIL", "");				//公司邮箱
//		map.put("QQ", "");					//qq
//		map.put("PERSONALEMAIL", "");		//个人邮箱
//		map.put("ZIPCODE", "");				//邮编
//		map.put("LINKADDR", "");			//联系地址
//		map.put("VPMNNO", "");				//V网短号
//		map.put("HOMEPHONE", "13800138001");			//家庭传真
//		map.put("NICKNAME", "");			//昵称
//		map.put("RANK", "");				//职位
//		map.put("PERSONALWEB", "");			//个人主页
//		map.put("MARRIAGE", "");			//婚姻状态
//		map.put("BIRTHDAY", "");			//生日
//		map.put("COUNTRY", "");				//所在国家
//		map.put("COMPANYURL", "");			//公司主页
//		map.put("COMPANYADDRESS", "");		//公司地址
//		syncUserCustomer("syncUserCustomer", map);
		
		//用户登录接口
//		Map<String, String> map = new TreeMap<String, String>();
//		map.put("LOGINTYPE", "0");
//		map.put("USERNAME", "15900000002");		//对应的ID 1004
//		map.put("PASSWORD", "Aa888888");
//		login("login", map);
		
		//修改密码
//		Map<String, String> map = new TreeMap<String, String>();
//		map.put("USERID", "1004");
//		map.put("PASSWORD", "8888");
//		map.put("NEWPASSWORD", "Aa888888A");
//		modifyPassword("modifyPassword", map);
	}
	
	
	private static StringBuffer deptBuffer = new StringBuffer("1,");
	private static List findChild(Element deptList, String dID){
		Iterator ite = deptList.elementIterator();
		int i =1;
		int j =1;
		String id = null;
		String  id1 = null;
		while(ite.hasNext()) {
			
			Element deptInfo = (Element)ite.next();
			id = deptInfo.elementText("DEPTPARENT");	//父ID
			id1 = deptInfo.elementText("DEPTID");	//部门ID
			if(dID.equals(id)){
//				deptBuffer.append(i+",");
//				System.out.println(id1+": " +deptBuffer.toString());
				if(parentID.contains(id1)) {
					ite = deptList.elementIterator();
					findChild(deptList, id1);
//					deptBuffer.append(i+",");
				}
//				i++;
			}
		}
		return null;
	}
	
	/**
	 * 用户登录接口
	 * @param method
	 * @param map
	 * @return
	 */
	public static String login(String method, Map<String, String> map) {
		return query(method, map);
	}
	
	public static String modifyPassword(String method, Map<String, String> map) {
		return query(method, map);
	}
	
	/**
	 * 企业信息查询接口
	 * @return
	 */
	public static String ecInfoQuery(String method){
		Document doc = wsd.getReqXmlTemplate(method);
		//设置head
		Element head = doc.getRootElement().element("HEAD");
		head.element("CODE").addText(method);
    	head.element("SID").addText("6786451");
    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
    	head.element("TIMESTAMP").addText(reqTime);
    	//设置body
    	Map<String, String> map = new TreeMap<String, String> ();
    	map.put("ECID", "1000");
    	doc.getRootElement().element("BODY").setText(wsd.setBody(map));
    	System.out.println("请求加密模版："+wsd.getXMLStr(doc));
		return wsd.requestSend(wsd.getXMLStr(doc), method);
	}
	
	/**
	 * 企业信息同步接口
	 * @param map
	 * @return
	 */
	public static String ecInfoSync(String method, Document document){
		Document doc = wsd.getReqXmlTemplate(method);
		//设置head
		Element head = doc.getRootElement().element("HEAD");
		head.element("CODE").addText(method);
    	head.element("SID").addText("6786451");
    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
    	head.element("TIMESTAMP").addText(reqTime);
    	//设置body
    	doc.getRootElement().element("BODY").setText(EncodeUtil.encrypt(wsd.getXMLStr(document), wsd.TEST_KEY));
    	System.out.println("请求完整的参数：++++++" +wsd.getXMLStr(doc));
    	return wsd.requestSend(wsd.getXMLStr(doc), method);
	}
	/**
	 * 3.1.13	个人信息查询接口
	 * @param method
	 * @return
	 */
	public static String userInfoQuery(String method, Map<String, String> map) {
		return query(method, map);
	}
	
	/**
	 * 3.1.19	企业部门查询接口
	 * @param method
	 * @param doucment
	 * @return
	 */
	public static String deptQuery(String method,Document document) {
//		Document doc = wsd.getReqXmlTemplate(method);
//		//设置head
//		Element head = doc.getRootElement().element("HEAD");
//		head.element("CODE").addText(method);
//    	head.element("SID").addText("6786451");
//    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
//    	head.element("TIMESTAMP").addText(reqTime);
//    	//设置body
//    	doc.getRootElement().element("BODY").setText(EncodeUtil.encrypt(wsd.getXMLStr(document), wsd.TEST_KEY));
//    	return wsd.requestSend(wsd.getXMLStr(doc), method);
		
		return query(method, document);
	}
	
	/**
	 * 3.1.31	企业部门同步接口
	 * @param method
	 * @param map
	 * @return
	 */
	public static String deptSync(String method, Map<String, String> map){
		Document doc = DocumentHelper.createDocument();
		Element body = doc.addElement("BODY");
		wsd.setElement(body.addElement("DEPTINFO "), map);
		return query(method, doc);
	}
	
	/**
	 * 3.1.20	企业部门详情查询接口
	 * @param method
	 * @return
	 */
	public static String deptDetail(String method, Map<String, String> map) {
		return query(method, map);
	}
	
	/**
	 * 3.1.14	个人信息同步接口
	 * @param method
	 * @return
	 */
	public static String userInfoSync(String method,Map<String, String> userMap, Map<String, String> memberMap) {
		Document doc = wsd.getReqXmlTemplate(method);
		//设置head
		Element head = doc.getRootElement().element("HEAD");
		head.element("CODE").addText(method);
    	head.element("SID").addText("1267349000");
    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
    	head.element("TIMESTAMP").addText(reqTime);
    	//设置body
    	Element body = doc.getRootElement().element("BODY");
    	//生成文档并加密
    	Document document = DocumentHelper.createDocument();
    	Element b = document.addElement("BODY");
    	wsd.setElement(b.addElement("USER"),userMap);
    	wsd.setElement(b.addElement("MEMBER"),memberMap);
    	System.out.println(wsd.getXMLStr(document));
    	String result = EncodeUtil.encrypt(wsd.getXMLStr(document), wsd.TEST_KEY);
    	body.setText(result);
    	System.out.println("请求模版：++++++"+wsd.getXMLStr(doc));
		return wsd.requestSend(wsd.getXMLStr(doc), method);
	}
	
	/**
	 * 3.1.21	个人通讯录查询
	 * @param method
	 * @param document
	 * @return
	 */
	public static String personalAddressQuery(String method, Document document) {
		return query(method, document);
	}
	
	/**
	 * 3.1.15	企业通讯录信息查询接口
	 * @param method
	 * @param pageInfoMap
	 * @return
	 */
	public static String addressQuery(String method, Document document) {
		
    	return query(method, document);
	}
	
	/**
	 * 3.1.35个人通讯录同步接口
	 * @param method 接口所调用的方法名
	 * @param map userCustomer
	 * @return
	 */
	public static String syncUserCustomer(String method, Map<String, String> map) {
		Document document = DocumentHelper.createDocument();
		Element body = document.addElement("BODY");
		wsd.setElement(body.addElement("USERCUSTOMER"), map);
		return query(method, document);
	}
	
	/**
	 * 3.1.16	企业通讯录信息详情查询接口
	 * @param method
	 * @param map
	 * @return
	 */
	public static String addressDetail(String method, Map<String, String> map) {
		return query(method, map);
	}
	
	/**
	 * 3.1.33	企业通讯录同步接口
	 * @param method
	 * @param userMap
	 * @param memberMap
	 * @return
	 */
	public static String syncMember(String method, Map<String, String> userMap, Map<String, String> memberMap, String ecid,
									String userid,String oprtype) {
		
		Document doc = wsd.getReqXmlTemplate(method);
		//设置head
		Element head = doc.getRootElement().element("HEAD");
		head.element("CODE").addText(method);
    	head.element("SID").addText("1267349000");
    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
    	head.element("TIMESTAMP").addText(reqTime);
    	//设置body
    	Element body = doc.getRootElement().element("BODY");
    	//生成文档并加密
    	Document document = DocumentHelper.createDocument();
    	Element b = document.addElement("BODY");
    	b.addElement("ECID").setText(ecid);
    	b.addElement("USERID").setText(userid);
    	b.addElement("OPRTYPE").setText(oprtype);
    	wsd.setElement(b.addElement("USER"),userMap);
    	wsd.setElement(b.addElement("MEMBER"),memberMap);
    	System.out.println(wsd.getXMLStr(document));
    	String result = EncodeUtil.encrypt(wsd.getXMLStr(document), wsd.TEST_KEY);
    	body.setText(result);
    	System.out.println("请求模版：++++++"+wsd.getXMLStr(doc));
		return wsd.requestSend(wsd.getXMLStr(doc), method);
		
	}
	
	private static String query(String method, Map<String,String> map) {
		Document doc = wsd.getReqXmlTemplate(method);
		//设置head
		Element head = doc.getRootElement().element("HEAD");
		head.element("CODE").addText(method);
    	head.element("SID").addText("6786451");
    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
    	head.element("TIMESTAMP").addText(reqTime);
    	//设置body
    	doc.getRootElement().element("BODY").setText(wsd.setBody(map));
    	System.out.println("请求加密模版："+wsd.getXMLStr(doc));
		return wsd.requestSend(wsd.getXMLStr(doc), method);
	}
	private static String query(String method,Document document) {
		Document doc = wsd.getReqXmlTemplate(method);
		//设置head
		Element head = doc.getRootElement().element("HEAD");
		head.element("CODE").addText(method);
    	head.element("SID").addText("6786451");
    	String reqTime=new SimpleDateFormat("yyyyMMddHHmmsssss").format(new Date());
    	head.element("TIMESTAMP").addText(reqTime);
    	//设置body
    	doc.getRootElement().element("BODY").setText(EncodeUtil.encrypt(wsd.getXMLStr(document), wsd.TEST_KEY));
    	return wsd.requestSend(wsd.getXMLStr(doc), method);
	}
}
